<?php

/**
 * This is the model class for table "log_action".
 *
 * The followings are the available columns in table 'log_action':
 * @property string $id_log_action
 * @property string $id_group
 * @property string $id_host
 * @property string $id_user_role
 * @property string $id_user
 * @property string $id_account
 * @property string $id_cpanel_accounts
 * @property string $action_event
 * @property string $extra_info
 * @property string $time_date
 */
class LogAction extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return LogAction the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'log_action';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_group, id_host, id_user_role, id_user, id_account, id_cpanel_accounts', 'length', 'max'=>10),
			array('action_event, extra_info', 'length', 'max'=>200),
			array('time_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_log_action, id_group, id_host, id_user_role, id_user, id_account, id_cpanel_accounts, action_event, extra_info, time_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'User' => array(self::HAS_ONE, 'User', array('id_user' => 'id_user')),
			'Group' => array(self::HAS_ONE, 'Group', array('id_group' => 'id_group')),
			'Host' => array(self::HAS_ONE, 'Host', array('id_host' => 'id_host')),
			'UserRole' => array(self::HAS_ONE, 'UserRole', array('id_user_role' => 'id_user_role')),
			'WhmAccount' => array(self::HAS_ONE, 'WhmAccount', array('id_account' => 'id_account')),
			'CpanelAccount' => array(self::HAS_ONE, 'CpanelAccounts', array('id_cpanel_accounts' => 'id_cpanel_accounts')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_log_action' => 'Id Log Action',
			'id_group' => 'Id Group',
			'id_host' => 'Id Host',
			'id_user_role' => 'Id User Role',
			'id_user' => 'Id User',
			'id_account' => 'Id Account',
			'id_cpanel_accounts' => 'Id Cpanel Accounts',
			'action_event' => 'Action Event',
			'extra_info' => 'Extra Info',
			'time_date' => 'Time Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_log_action',$this->id_log_action,true);
		$criteria->compare('id_group',$this->id_group,true);
		$criteria->compare('id_host',$this->id_host,true);
		$criteria->compare('id_user_role',$this->id_user_role,true);
		$criteria->compare('id_user',$this->id_user,true);
		$criteria->compare('id_account',$this->id_account,true);
		$criteria->compare('id_cpanel_accounts',$this->id_cpanel_accounts,true);
		$criteria->compare('action_event',$this->action_event,true);
		$criteria->compare('extra_info',$this->extra_info,true);
		$criteria->compare('time_date',$this->time_date,true);

		$criteria->with = array('User', 'Group', 'Host', 'UserRole', 'WhmAccount', 'CpanelAccount');
		
		$postData = app()->request->getParam('ActionActivitySearchForm', null);
		if($postData){
			if(!empty($postData['conditionVal'])){
				$conditionVal = $postData['conditionVal'];
//				$conditionVal = str_replace('-', '', $conditionVal);
				$conditionVal = str_replace('  ', ' ', $conditionVal);
				$conditionVal = trim($conditionVal);
				
				if($postData['conditionField'] == 'username'){
					$criteria->addSearchCondition('User.username', $conditionVal);
				}else if($postData['conditionField'] == 'id_log_action'){
					$criteria->addSearchCondition('t.id_log_action', $conditionVal);
				}else if($postData['conditionField'] == 'action'){
					$criteria->addSearchCondition("t.action_event", $conditionVal, true, 'OR');
					$criteria->addSearchCondition("Group.name", $conditionVal, true, 'OR');
					$criteria->addSearchCondition("Host.name", $conditionVal, true, 'OR');
					$criteria->addSearchCondition("UserRole.name", $conditionVal, true, 'OR');
					$criteria->addSearchCondition("WhmAccount.domain", $conditionVal, true, 'OR');
					$criteria->addSearchCondition("CpanelAccount.domain", $conditionVal, true, 'OR');
					$criteria->addSearchCondition("t.extra_info", $conditionVal, true, 'OR');
				}
			}
			if(!empty($postData['dateFrom'])){
				$startDate = date('Y-m-d 00:00:00', strtotime($postData['dateFrom']));
				$criteria->addCondition("t.time_date>='{$startDate}'");
			}
			if(!empty($postData['dateTo'])){
				$endDate = date('Y-m-d 23:59:59', strtotime($postData['dateTo']));
				$criteria->addCondition("t.time_date<='{$endDate}'");
			}
		}
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort' => array(
				'defaultOrder'=>'time_date DESC',
				'attributes' => array(
					'time_sort' => array(
						'asc' => 't.time_date ',
						'desc' => 't.time_date DESC',
					)
				)
			),
			'pagination' => array(
				'pageSize'=> app()->user->getData()->getIndividualConfig()->getPagination()
			)
		));
	}
	
	public function __get($key)
	{
		$action = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
		if(method_exists($this, $action)){
			return $this->$action();
		}
		
		return parent::__get($key);
	}
	public function getStatusWithExtraInfo()
	{
		$ret = $this->action_event;
		if(!empty($this->id_cpanel_accounts)){
			if(empty($this->CpanelAccount)){
				$ret .= ' | Terminated Cpanel Account';
			}else{
				$ret .= ' | ' . $this->CpanelAccount->domain;
			}
		}else if(!empty($this->id_account)){
			if(empty($this->WhmAccount)){
				$ret .= ' | Deleted WhmAccount';
			}else{
				$ret .= ' | ' . $this->WhmAccount->domain;
			}
		}else if(!empty($this->id_group)){
			if(empty($this->Group)){
				$ret .= ' | Deleted Group';
			}else{
				$ret .= ' | ' . $this->Group->name;
			}
		}else if(!empty($this->id_host)){
			if(empty($this->Host)){
				$ret .= ' | Deleted Host';
			}else{
				$ret .= ' | ' . $this->Host->name;
			}
		}else if(!empty($this->id_user_role)){
			if(empty($this->UserRole)){
				$ret .= ' | Deleted UserRole';
			}else{
				$ret .= ' | ' . $this->UserRole->name;
			}
		}else if(!empty($this->id_user)){
			if(empty($this->User)){
				$ret .= ' | Deleted User';
			}else{
				$ret .= ' | ' . $this->User->fullname;
			}
		}
		
		if(in_array($this->action_event, array(SystemLog::CP_BWL, SystemLog::CP_SUSPENDACC, SystemLog::CP_TERMIACC))){
			if(!empty($this->extra_info)){
				$ret .= ' - ' . $this->extra_info;
			}
		}
		
		return $ret;
	}
	
	public function getUsernameShow()
	{
		return empty($this->User)? 
			"Deleted User" : 
			$this->User->username;
	}
	
	public function purge($beforeTime)
	{
		if(is_int($beforeTime)){
			$beforeTime = date('Y-m-d H:i:s', $beforeTime);
		}
		
		$criteria = new CDbCriteria;
		$criteria->addCondition("time_date<'{$beforeTime}'");
		
		return $this->deleteAll($criteria);
	}
}